#!/bin/sh
#
# DracoPKG
#
# Copyright 2008 Ole Andre Rodlie <olear@dracolinux.org>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

function dP_update_system () {
      if [ -a ${DRACOSRC_CACHE}/FILELIST.TXT ]; then
        REMOTE=`cat ${DRACOSRC_CACHE}/FILELIST.TXT | awk '{print $8}' | sed 's#/# #g;s#.tgz##g' | awk '{print $3}'`
        PKG_CRITICAL="framework etc pkgtools dracopkg glibc attr acl termcap-compat pcre db db-compat tnftp grep sed nvi"
        for cpkg in ${PKG_CRITICAL}; do
          CPKG="`cat ${DRACOSRC_CACHE}/FILELIST.TXT | awk '{print $8}' | sed 's#/# #g;s#.tgz##g' | awk '{print $3}'| sed '/'${cpkg}'-[0-9]/!d'`"
          if [ "${CPKG}" ]; then
            LOCAL=`( cd ${DRACOSRC_LOG} ; ls | sed '/'${CPKG}'/!d')`
            if [ ! "${LOCAL}" ]; then
              if [ -a ${DRACOSRC_CACHE}/${CPKG}.tgz ]; then
                  ${PKGTOOLS_UPGRADE} ${DRACOSRC_CACHE}/${CPKG}.tgz
              else
                PKG_URL=`cat ${DRACOSRC_CACHE}/FILELIST.TXT | sed '/'${CPKG}'/!d' | awk '{print $8}' | sed 's#./##'`
                ( cd ${DRACOSRC_CACHE} ; ${GET} ${MIRROR}/draco-${VERSION}/draco/${PKG_URL} )
                if [ -a ${DRACOSRC_CACHE}/${CPKG}.tgz ]; then
                  ${PKGTOOLS_UPGRADE} ${DRACOSRC_CACHE}/${CPKG}.tgz
                else
                  echo "Package failed to download."
                  exit 1
                fi
              fi
            fi
          fi
        done        
        for pkg in ${REMOTE}; do
          LOCAL=`( cd ${DRACOSRC_LOG} ; ls | sed '/'${pkg}'/!d')`
          if [ ! "${LOCAL}" ]; then
            PKG_CATEGORY=`cat ${DRACOSRC_CACHE}/FILELIST.TXT | sed '/'${pkg}'/!d' | awk '{print $8}' | sed 's#/# #g;s#.tgz##g' | awk '{print $2}'`
            echo "* Remote package [${PKG_CATEGORY}] ${pkg} not installed."
            read -p "* Install package (Y/n)? "
            if [ ! "${REPLY}" = "n" ]; then
              if [ -a ${DRACOSRC_CACHE}/${pkg}.tgz ]; then
                ${PKGTOOLS_UPGRADE} ${DRACOSRC_CACHE}/${pkg}.tgz
              else
                PKG_URL=`cat ${DRACOSRC_CACHE}/FILELIST.TXT | sed '/'${pkg}'/!d' | awk '{print $8}' | sed 's#./##'`
                ( cd ${DRACOSRC_CACHE} ; ${GET} ${MIRROR}/draco-${VERSION}/draco/${PKG_URL} )
                if [ -a ${DRACOSRC_CACHE}/${pkg}.tgz ]; then
                  ${PKGTOOLS_UPGRADE} ${DRACOSRC_CACHE}/${pkg}.tgz
                else
                  echo "Package failed to download."
                  exit 1
                fi
              fi
            fi             
          fi
        done
        LOCAL=`( cd ${DRACOSRC_LOG} ; ls )`
        for pkg in ${LOCAL}; do
          REMOTE=`cat ${DRACOSRC_CACHE}/FILELIST.TXT | awk '{print $8}' | sed 's#/# #g;s#.tgz##g' | awk '{print $3}' | sed '/'${pkg}'/!d'`
          if [ ! "${REMOTE}" ]; then
            echo "* Local package ${pkg} not in package list."
            read -p "* Remove package (Y/n)? "
            if [ ! "${REPLY}" = "n" ]; then
              ${PKGTOOLS_REMOVE} ${pkg}
            fi
          fi
        done
        for dotnew in $(find /etc -type f -name "*.new" -maxdepth 2) ; do
          echo "New file found: ${dotnew}"
        done
        for dotnew in $(find /usr/local/etc -type f -name "*.new" -maxdepth 2) ; do
          echo "New file found: ${dotnew}"
        done
      else
        echo
        echo "Package list not found."
        echo "Please run '${DPN} audit update'."
        echo
        exit 1
      fi      
}
